package com.xingzhe.pengji.security.services.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xingzhe.pengji.security.dao.AuthoriteIbatisDao;
import com.xingzhe.pengji.security.domain.Authorite;
import com.xingzhe.pengji.security.services.AuthoriteService;

/**
 * @Title ： 三江学院 毕业设计
 * @Description ：
 * @author ：LuFengLiang
 * @Company: IT行者工作室
 * @Copyright: Copyright (c) 2012-2-24
 * @version ： 1.0
 * @param <T>
 */
@Service("authoriteService")
public class AuthoriteServiceImpl<T extends Authorite> implements AuthoriteService<T>
{

    private static final Logger logger = LoggerFactory.getLogger(AuthoriteServiceImpl.class);
    @Autowired
    private AuthoriteIbatisDao<T> authoriteIbatisDao;

    @Override
    public List<T> queryForPageAuthoriteList(T entity, String sortByColumn, int pageSize, int pageNumber,
            boolean sortAsc)
    {
        return authoriteIbatisDao.queryForPaginatedList(organizeParams(entity), pageNumber, pageSize, sortByColumn,
                sortAsc);
    }

    @Override
    public void saveAuthorite(T entity)
    {
        boolean b = authoriteIbatisDao.addAuthorite(entity);
        if ( !b )
        {
            logger.error("新增权限失败");
        }
    }

    @Override
    public void updateAuthorite(T entity)
    {
        boolean b = authoriteIbatisDao.updateAuthorite(entity);
        if ( !b )
        {
            logger.error("更新权限失败！");
        }
    }

    @Override
    public void delAuthorite(T entity)
    {
        entity.setDelFlag(-1);
        boolean b = authoriteIbatisDao.updateAuthorite(entity);
        if ( !b )
        {
            logger.error("删除权限失败！");
        }
    }

    @Override
    public T getAuthoriteByName(T entity)
    {
        T authorite = authoriteIbatisDao.getAuthoriteByName(entity);
        if ( null != authorite )
        {
            return authorite;
        }
        else
        {
            return authorite;
        }
    }

    @Override
    public T getAuthoriteById(T entity)
    {
        T authorite = authoriteIbatisDao.getAuthoriteById(entity);
        if ( null != authorite )
        {
            return authorite;
        }
        else
        {
            logger.error("该权限不存在");
            return authorite;
        }
    }

    @Override
    public int getItemCount(T entity)
    {
        return authoriteIbatisDao.getItemCount(organizeParams(entity));
    }

    @SuppressWarnings("unchecked")
    private Map<String, Object> organizeParams(T entity)
    {
        if ( null == entity )
        {
            entity = (T) new Authorite();
        }
        if ( null != entity.getName() && ("".equals(entity.getName().trim())) )
        {
            entity.setName(null);
        }
        // 用于存放参数值的容器，用于Ibatis条件查询是取值
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", entity.getId());
        params.put("name", entity.getName());
        params.put("description", entity.getDescription());

        params.put("enabled", entity.getEnabled());

        return params;
    }

	@Override
	public List<String> getAllAuthoritieName() {
		return authoriteIbatisDao.getAllAuthoritieName();
	}

}
